package com.bgba.oer.service;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.Extraction;
import com.flashline.registry.openapi.entity.Project;
import com.flashline.registry.openapi.entity.ProjectEntities;
import com.flashline.registry.openapi.entity.RegistryUser;
import com.flashline.registry.openapi.query.ProjectCriteria;
import com.flashline.registry.openapi.query.UserCriteria;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import
com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;
public class RegistryUserService {
	
	public static void main(String pArgs[]) throws ServiceException, ParserConfigurationException, SAXException, IOException  {
		try { 

			System.out.println("======================== ");
			String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
			String username = " L0269239";
			String password = "finde2011";
			/*
			String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
			String username = " admin";
			String password = "admin123";
			 */

			///////////////////////////////////////////////////////////
			// Connect to Oracle Enterprise Repository
			///////////////////////////////////////////////////////////
			URL lURL = null;
			lURL = new URL(urlConnectionLogin);
			FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

			///////////////////////////////////
			// Login to OER
			///////////////////////////////////
			AuthToken authToken = repository.authTokenCreate(username, password);

			//String registryUsername ="L0269239";
			String registryUsername ="Alberto Tiberti";
			
			
			searchRegistryUser(repository, authToken, registryUsername);


		} catch (Exception lEx) {
			System.err.println("####################### ERROR!!!! ="+lEx.getMessage());
			lEx.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param repository
	 * @param authToken
	 * @param registryUsername
	 * @return
	 * @throws RemoteException
	 * @throws OpenAPIException
	 */
	public static RegistryUser searchRegistryUser(FlashlineRegistry repository,	AuthToken authToken, String registryUsername)	throws RemoteException, OpenAPIException {
		
		System.out.println("================================ Searching registryUser with fullName="+registryUsername);
		
		// -----------------------------------------
		// Query for Users
		RegistryUser myUsers[] = null;
		UserCriteria myUserCriteria = null;
		myUserCriteria = new UserCriteria();
		myUserCriteria.setNameCriteria(registryUsername);		
		
		myUsers = repository.userQuery(authToken,   myUserCriteria);
		
		RegistryUser myRegistryUser = null;		

		if (myUsers.length > 0) {
			if (myUsers.length == 1) {
				myRegistryUser = (RegistryUser) myUsers[0];
				System.out.println("================================ myRegistryUser.getUserName="+myRegistryUser.getUserName()); 
				System.out.println("================================ myRegistryUser.getEmail="+myRegistryUser.getEmail()); 
				System.out.println("================================ myRegistryUser.getFirstName="+myRegistryUser.getFirstName()); 
				System.out.println("================================ myRegistryUser.getLastName="+myRegistryUser.getLastName()); 
			} else {
				System.out.println("======================================= WARN!!! RegistryUser duplicated!!! resultSet="+myUsers.length);
				String[] messageArgs = {"Error, RegistryUser duplicated"};
				throw new OpenAPIException(messageArgs, 1, "", "", "ERROR" );
			}
		} else {
			System.out.println("======================================= NOT Found RegistryUser for criteriaName="+registryUsername);
		}
		
		return myRegistryUser;
	}
}
